package src.util;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.logging.Logger;

public class Messageitem
{
	// ---------------------------------------------------------------------------------------------

	private static MessageCfg		messageCfg	= null;

	private String						name			= null;
	private String						data			= null;

	// ---------------------------------------------------------------------------------------------

	public enum MessageType {
		String, xml
	};

	// ---------------------------------------------------------------------------------------------

	public Messageitem( MessageCfg messageCfg)
	{
		Messageitem.messageCfg = messageCfg;
	}

	// ---------------------------------------------------------------------------------------------

	public Messageitem( String name, String data)
	{
		this( name, data, false);
	}

	// ---------------------------------------------------------------------------------------------

	public Messageitem( String name, String data, boolean fromNet)
	{
		if( fromNet && messageCfg.isEncoded( ))
		{
			try
			{
				String tempName = URLDecoder.decode( name, messageCfg.getConverter( ));
				String tempData = URLDecoder.decode( data, messageCfg.getConverter( ));
				this.name = tempName;
				this.data = tempData;

			}
			catch( UnsupportedEncodingException ex)
			{
				return;
			}
		}
		else
		{
			this.name = name;
			this.data = data;
		}
	}

	// ---------------------------------------------------------------------------------------------

	public String getName( )
	{
		return name;
	}

	// ---------------------------------------------------------------------------------------------

	public String getData( )
	{
		return data;
	}

	// ---------------------------------------------------------------------------------------------

	public void setData( String data)
	{
		this.data = data;
	}

	// ---------------------------------------------------------------------------------------------

	public String toString( )
	{
		if( messageCfg.isEncoded( ))
		{
			try
			{
				return URLEncoder.encode( name, messageCfg.getConverter( )) + "="
							+ URLEncoder.encode( data, messageCfg.getConverter( ));
			}
			catch( UnsupportedEncodingException ex)
			{
				return null;
			}
		}
		return "" + name + "=" + data;
	}
	// ---------------------------------------------------------------------------------------------
}
